#include "BaseMoveController.h"

BaseMoveController::BaseMoveController(){
	m_entity = NULL;
}

BaseMoveController::~BaseMoveController(){
	CC_SAFE_RETAIN(m_entity);
}

BaseMoveController* BaseMoveController::create(Entity* entity){
	BaseMoveController* baseMoveController = new BaseMoveController();

	if(baseMoveController && baseMoveController->init(entity)){
		baseMoveController->autorelease();

	} else{
		CC_SAFE_DELETE(baseMoveController);
		baseMoveController = NULL;
	}

	return baseMoveController;
}

bool BaseMoveController::init(Entity* entity){
	bool bRet = false;
	do{
		CC_SAFE_RETAIN(entity);
		CC_SAFE_RELEASE(m_entity);
		m_entity = entity;
		

		bRet = true;
	}while(0);

	return bRet;
}

void BaseMoveController::moveByTouch(float moveLen){
	if (m_entity != NULL){
		CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
		CCSize spriteSize = m_entity->getSprite()->getContentSize();

		float leftLine = spriteSize.width/2;
		float rightLine = visibleSize.width - spriteSize.width/2;

		float curX = m_entity->getPositionX()+moveLen;

		if(curX > rightLine){
			curX = rightLine;
		} else if(curX < leftLine){
			curX = leftLine;
		}
		
		m_entity->setPositionX(curX);
	}
}